package net.capgemini.bill.gaty.expressionarithmetique.web.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.capgemini.bill.gaty.expressionarithmetique.web.enums.FonctionType;
import net.capgemini.bill.gaty.expressionarithmetique.web.enums.RequestType;
import net.capgemini.bill.gaty.expressionarithmetique.web.ws.client.WsClient;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MainController {
	@Autowired(required=true)
	private WsClient client;
	private static Logger logger=LoggerFactory.getLogger(MainController.class);
@RequestMapping(method=RequestMethod.GET,value="/index")
	public Object index(){
	ModelAndView m=new ModelAndView("index");
	List<RequestType> liste = Arrays.asList(RequestType.values());
	m.addObject("listerequestType", liste);
	logger.info("listerequestType="+liste+",client="+client);
		return m;
	}
@RequestMapping(method=RequestMethod.GET,value="/fromajax")
public void index_1( HttpServletRequest request,HttpServletResponse response) throws IOException{
	logger.info(" ,client="+client);
	  index_0(   request,  response) ;
}
@RequestMapping(method=RequestMethod.POST,value="/fromajax")
public void index_0( HttpServletRequest request,HttpServletResponse response) throws IOException{
	 String name=((String) request.getParameter("name")).trim();
	 String expression=((String) request.getParameter("expression")).trim();
	 String variable=((String) request.getParameter("variable")).trim();
	RequestType requesType=RequestType.valueOf(RequestType.class, name);
	logger.info(String.format("name=%s,expression=%s,variable=%s", name,expression,variable));
	Method m=null;
	 PrintWriter writer=response.getWriter();
	 Object result = null;
		try {
			if(requesType.getType()==FonctionType.BINAIRE){
			m=client.getClass().getDeclaredMethod(requesType.getFunctionName(), requesType.getFirstArgType(),requesType.getSecondArgType());
			if(requesType.getSecondArgType()==double[].class){
				//variable x=xvalue,y=yvalue,z=Zvalue
				String[] vals=variable.split(",");
				double [] res=new double[vals.length];
				int i=0;
				for (String v : vals) {
					res[i++]=Double.parseDouble(v.split("=")[1].trim());
				}
				result=m.invoke(client,expression,res);
				
				
			}else{
				result=m.invoke(client,expression,variable);
				 
			}
			
			}
			else if(requesType.getType()==FonctionType.UNAIRE){
				m=client.getClass().getDeclaredMethod(requesType.getFunctionName(), requesType.getFirstArgType());
				result=m.invoke(client,expression);
				 
			}
			else if(requesType.getType()==FonctionType.NOPAREMETER){
				m=client.getClass().getDeclaredMethod(requesType.getFunctionName());
				result=m.invoke(client);
				 
			}
			logger.info("result="+result);
			writer.print(result);
		
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.info(e.getMessage());
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.info(e.getMessage());
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.info(e.getMessage());
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.info(e.getMessage());
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
e.printStackTrace();
logger.info(e.getMessage());
		}
}
}
